{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "fb58a416-c8e6-43e0-90de-0ba04ec3072c",
   "metadata": {},
   "source": [
    "# 将keras模型转换成tflite模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "880c6bac-b342-4122-8adb-33ee711bef40",
   "metadata": {},
   "outputs": [],
   "source": [
    "import keras\n",
    "import tensorflow as tf"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2d998206-1a38-43fd-9a60-844f43e4dd31",
   "metadata": {},
   "source": [
    "### 从文件中加载keras模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "06539fed-2b11-4472-bc42-30df50bfc84b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model: \"sequential\"\n",
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "dense (Dense)                (None, 32)                64        \n",
      "_________________________________________________________________\n",
      "dense_1 (Dense)              (None, 64)                2112      \n",
      "_________________________________________________________________\n",
      "dense_2 (Dense)              (None, 1)                 65        \n",
      "=================================================================\n",
      "Total params: 2,241\n",
      "Trainable params: 2,241\n",
      "Non-trainable params: 0\n",
      "_________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "model = keras.models.load_model('sin.model.h5')\n",
    "model.summary()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7afc71ad-775c-4ea7-9a0f-8662a867e4c8",
   "metadata": {},
   "source": [
    "### 从keras模型转换到tflite模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e96ccde6-5108-4813-95d1-47827d00aa68",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:tensorflow:Assets written to: C:\\Users\\sheng\\AppData\\Local\\Temp\\tmpkxydx7qq\\assets\n"
     ]
    }
   ],
   "source": [
    "converter = tf.lite.TFLiteConverter.from_keras_model(model)\n",
    "tflite_model = converter.convert()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "57cfd4ef-1ef1-484a-9ab7-6119d4ff770b",
   "metadata": {},
   "source": [
    "### 将转换后的模型写入文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c9f57f45-3636-4ed8-b9cc-5e287d4efb47",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('sin.tf_lite.model','wb') as f:\n",
    "    f.write(tflite_model)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "python3 (tf_gpu)",
   "language": "python",
   "name": "conda-env-tf_gpu-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
